package org;

import org.entities.LevelObject;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class GameState extends BasicGameState {

    public static float mapX = 0;
    public static float mapY = 0;
    public static BlockMap bmap;
    public Input input;
    public static boolean up,  down,  left,  right,  jump,  action1,  shoot;
    public static int viewWidth = 400;
    public static int viewHeight = 300;
    public static int viewX = 0;
    public static int viewY = 0;
    public static final int ID = 2;

    public int getID() {
        return ID;
    }

    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        container.setVSync(true);
        input = container.getInput();
        bmap = new BlockMap("data/level/level0001.tmx");
        adjustCam();
        container.setShowFPS(true);
    }

    public static void adjustCam() {
        
        mapX = 200 - bmap.Samus.x;
        mapY = 150 - bmap.Samus.y;

        if (mapX > 0) {
            mapX = 0;
        }
        if (mapY > 0) {
            mapY = 0;
        }
        if (mapX < -(BlockMap.mapWidth - viewWidth)) {
            mapX = -(BlockMap.mapWidth - viewWidth);
        }
        if (mapY < -(BlockMap.mapHeight - viewHeight)) {
            mapY = -(BlockMap.mapHeight - viewHeight);
        }
    }

    public static void senseKey(GameContainer container) {
        if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
            left = true;
        } else {
            left = false;
        }
        if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {
            right = true;
        } else {
            right = false;
        }
        if (container.getInput().isKeyDown(Input.KEY_UP)) {
            up = true;
        } else {
            up = false;
        }
        if (container.getInput().isKeyDown(Input.KEY_DOWN)) {
            down = true;
        } else {
            down = false;
        }
        if (container.getInput().isKeyDown(Input.KEY_X)) {
            jump = true;
        } else {
            jump = false;
        }
        if (container.getInput().isKeyDown(Input.KEY_C)) {
            shoot = true;
        } else {
            shoot = false;
        }
        if (container.getInput().isKeyDown(Input.KEY_RETURN)) {
            action1 = true;
        } else {
            action1 = false;
        }

    }

    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {

        // check for keypresses
        senseKey(container);

        //update player
        bmap.Samus.update(delta);
            adjustCam();

            // update all entities
            for (int i = 0; i < bmap.entities.size(); i++) {
                LevelObject entity = (LevelObject) bmap.entities.get(i);
                entity.update(delta);
            }
        }        


    public void render(GameContainer container, StateBasedGame game, Graphics g) {

            // render background objects
            BlockMap.tmap.render(viewX + ((int) mapX % 16),
                    viewY + ((int) mapY % 16),
                    (int) (-mapX / 16),
                    (int) (-mapY / 16),
                    (viewWidth / 16) + 2,
                    (viewHeight / 16) + 2,
                    2, false);

            // render foreground objects
            BlockMap.tmap.render(viewX + ((int) mapX % 16),
                    viewY + ((int) mapY % 16),
                    (int) (-mapX / 16),
                    (int) (-mapY / 16),
                    (viewWidth / 16) + 2,
                    (viewHeight / 16) + 2,
                    1, false);

            g.translate(mapX, mapY);

            for (int i = 0; i < bmap.entities.size(); i++) {
                LevelObject entity = (LevelObject) bmap.entities.get(i);
                entity.draw(g);
            }
            g.resetTransform();
        }


    @Override
    public void keyReleased(int key, char c) {

        if (key == Input.KEY_ESCAPE) {
            System.exit(0);
        }
    }
}
